<!--增删字典信息的页面-->
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-row layui-col-space20">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">模型文件管理模块</div>
                        <div class="layui-card-body">
                            <div class="layui-row">
                                <div class="layui-col-md12">
                                    <fieldset class="layui-elem-field site-demo-button">
                                        <legend>新闻语料爬取</legend>
                                        <div class="layui-field-box">
                                            <label class="layui-form-label">时间选取：</label>
                                            <div class="layui-inline">
                                                <input type="text" class="layui-input" id="start">
                                            </div>
                                            -
                                            <div class="layui-inline">
                                                <input type="text" class="layui-input" id="end">
                                            </div>
                                            &nbsp;
                                            &nbsp;
                                            <div class="layui-inline">
                                                <div class="layui-btn-group">
                                                    <button type="button" onclick=" clawerNewByDate()"
                                                            class="layui-btn">
                                                        Step1:开始爬取
                                                    </button>
                                                    <button type="button" onclick=" doSegFromFile()"
                                                            class="layui-btn">
                                                        Step2:进行分词
                                                    </button>
                                                    <button type="button" onclick=" doTrainModel()"
                                                            class="layui-btn">
                                                        Step3:执行训练
                                                    </button>
                                                </div>
                                            </div>
                                        </div>

                                    </fieldset>
                                    <blockquote class="layui-elem-quote">
                                        LDA模型训练的三个步骤，分别是对语料的抓取更新、对格式化语料进行分词操作、最后是语料的LDA模型训练。
                                        <br>
                                        LDA语料来源于新浪新闻，分别从下面的几个新闻子栏目中提取：<br>
                                        科技、体育、军事、财经、娱乐、国际社会、健康、图书、教育、政治、社会、电影、科技
                                        <br>
                                        更多内容请阅读API文档中的说明。
                                    </blockquote>
                                </div>
                                <div class="layui-col-md12">
                                    <!--<table class="layui-hide" id="models_manage" lay-filter="models"></table>-->
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">已训练的模型及主题预览</div>
                        <div class="layui-card-body">
                            <div class="layui-row">
                                <div class="layui-col-md12">
                                    <fieldset class="layui-elem-field site-demo-button">
                                        <legend>主题模型预览</legend>
                                        <div class="layui-field-box">
                                            <table class="layui-hide" id="lda_models" lay-filter="lda_models"></table>
                                        </div>

                                    </fieldset>

                                </div>
                                <div class="layui-col-md12">

                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
    <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>

<script>
    layui.use('laydate', function () {
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
            , value: new Date(new Date().getTime() - 24 * 60 * 60 * 1000) //必须遵循format参数设定的格式
            , min: -2 //7天前
            , max: 0 //7天后
        });
        //执行一个laydate实例
        laydate.render({
            elem: '#end' //指定元素
            , value: new Date() //必须遵循format参数设定的格式
            , min: -1 //7天前
            , max: 0 //7天后
        });
    });

    //没有执行分词的
    var fileName;

    function clawerNewByDate() {

        var start = $('#start').val();
        var end = $('#end').val();
        var obj = new Object();
        obj.start = start;
        obj.end = end;
        var requestJson = JSON.stringify(obj);
        //加载动画
        var index = layer.load(2);
        jQuery.ajax({
            url: "/crawler/news.do",
            type: "post",
            data: requestJson,
            dataType: "json",
            contentType: "application/json;charset=utf-8"
        }).done(function (data) {
            console.log(data);
            layer.alert("爬取成功");

            //弹出窗口
            layer.open({
                type: 1,
                title: '新闻爬取结果展示',
                shade: 0,
                maxmin: true,
                skin: 'layui-layer-rim', //加上边框
                area: ['840px', '480px'], //宽高
                content: '<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">' +
                '  <legend>新闻结果数据集</legend>' +
                '</fieldset>' +
                ' ' +
                '      <pre class="layui-code">' +
                data.data[0].content +
                '      </pre>'
            });

            layui.use('code', function () {

                layui.code();
            });
            layer.close(index);
        }).fail(function (data) {
            layer.alert("请求失败", {icon: 7});
            layer.close(layerIndex);
        });

    }

    /**
     *执行分词
     */
    function doSegFromFile() {
        var obj = new Object();
        obj.fileName = fileName;
        //加载动画
        var index = layer.load(2);
        var requestJson = JSON.stringify(obj);
        jQuery.ajax({
            url: "/crawler/news/seg.do",
            type: "post",
            data: requestJson,
            dataType: "json",
            contentType: "application/json;charset=utf-8"
        }).done(function (data) {
            console.log(data);
            layer.alert("分词成功");
            layui.use('code', function () {
                layui.code();
            });
            layer.close(index);
        }).fail(function (data) {
            layer.alert("请求失败", {icon: 7});
            layer.close(layerIndex);
        });
    }


    /**
     * 训练模型
     */
    function doTrainModel() {

        var obj = new Object();
        obj.fileName = fileName;
        //加载动画
        var index = layer.load(2);
        var requestJson = JSON.stringify(obj);
        jQuery.ajax({
            url: "/crawler/news/train.do",
            type: "post",
            data: requestJson,
            dataType: "json",
            contentType: "application/json;charset=utf-8"
        }).done(function (data) {
            console.log(data);
            layer.alert("训练完成，请对照主题的词汇输入主题名称。并按确定键提交。");
            layui.use('code', function () {
                layui.code();
            });
            layer.close(index);
        }).fail(function (data) {
            layer.alert("请求失败", {icon: 7});
            layer.close(layerIndex);
        });x
    }


    var mydata;
    getModelsinfo();

    function getModelsinfo() {

        jQuery.ajax({
                url: "/crawler/news/models.do",
                type: "get",
                dataType: "json",
                contentType: "application/json;charset=utf-8"
            }).done(function (data) {
                mydata = data.data;
            }).fail(function (data) {
                layer.alert("请求失败", {icon: 7});
            layer.close(layerIndex);
        });

    }

    layui.use('table', function () {
        var table = layui.table;

        table.render({
            elem: '#lda_models'
            , data: mydata
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , cols: [[
                {field: 'name', align: 'center', title: '模型训练日期'}
                , {field: 'topics', title: '主题'}
                , {field: 'topicNames', title: '主题描述'}
                , {fixed: 'right', width: 178, align: 'center', toolbar: '#barDemo', title: "操作栏"}
            ]]
        });


        //监听工具条
        table.on('tool(lda_models)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                //弹出窗口
                layer.open({
                    type: 1,
                    title: '每个主题的描述',
                    shade: 0,
                    maxmin: true,
                    skin: 'layui-layer-rim', //加上边框
                    area: ['840px', '480px'], //宽高
                    content: '<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">' +
                    '  <legend>主题描述</legend>' +
                    '</fieldset>' +
                    ' ' +
                    '      <pre class="layui-code">' +
                    data.topics +
                    '      </pre>'
                });

            } else if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                layer.alert('编辑行：<br>' + JSON.stringify(data))
            }
        });
    });


</script>
